<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/12/6
 * Time: 17:23
 */

namespace app\admin\controller;
use think\Controller;

/**
 * Class Admin
 * @package app\admin\controller
 *
 */
class Admin extends Controller
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Admin();
    }
    //用户管理主页
    public function index(){
        $data = db('administrators')->select();
        //1视图
        return view('',compact('data'));
    }
    //用户新增
    public function add(){
        //2获取前台数据
        if(request()->isPost()){
            $data = input('post.');
            $res = $this->db->add($data);
            if ($res['valid']) {
                $this->success($res['msg'], 'index');
            } else {
                $this->error($res['msg']);
            }
        }
        //1视图
        return view();
    }
    //用户编辑
    public function edit(){
        //2分配数据
        $uid = input('param.uid');
        $oldData = db('user')->where('uid',$uid)->find();
        //编辑
        if(request()->isPost()){
            $data = input('post.');
            $res = $this->db->edit($data);
            if ($res['valid']) {
                $this->success($res['msg'], 'index');
            } else {
                $this->error($res['msg']);
            }
        }
        //1视图
        return view('',compact('oldData'));
    }
    //删除
    public function del(){
        $uid = input('param.uid');
        \app\common\model\Admin::destroy($uid);
        return $this->success('删除成功','index');
    }
    //分配权限
    public function setAuth(){
        $uid = input('param.uid');
        //2获取用户
        $userData = db('administrators')->where('uid',$uid)->find();
        //3获取用户组数据
        $groupData = db('auth_group')->select();
        //4分配权限
        if(request()->isPost()){
            $data = input('post.');
            $res = $this->db->setAuth($data);
            if ($res['valid']) {
                $this->success($res['msg'], 'index');
            } else {
                $this->error($res['msg']);
            }
        }
        //1视图
        return view('',compact('userData','groupData'));
    }
}